//获取应用实例
var app = getApp();
// var today = util.formatTime(new Date((new Date()).getTime() + (1000 * 60 * 60 * 24 * 1))).split(' ')[0];
// var minday = util.formatTime(new Date()).split(' ')[0];
// var maxday = util.formatTime(new Date((new Date()).getTime() + (1000 * 60 * 60 * 24 * 62))).split(' ')[0];
var util = require('../../utils/util.js');
var tcity = require("../../utils/citys.js");

Page({
    data: {
        carTypes: ['A1', 'A2', 'A3', 'B1', 'B2', 'C1', 'C2', 'C3', 'C4'],
    },
    selecttype: function (e) {
        this.setData({
            'carType': e.detail.value
        })
    },
    bindDateChange: function (e) {
        this.setData({
            registrationDate: e.detail.value
        })
    },
    bindDateChange1: function (e) {
        this.setData({
            expiryDate: e.detail.value
        })
    },
    dateAvatar1: function () {
        var that = this;
        wx.chooseImage({
            count: 1,
            success: function (res) {
                var tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: 'http://www.ppxpc.com/api/wx/common/imageUpload',
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {

                    },
                    success: function (result) {
                        if (result.statusCode == 200){
                            wx.showToast({
                                title: '上传成功',
                                icon: 'success',
                                duration: 2000
                            })
                            that.setData({
                                img1:result.data
                            })
                        }

                    },
                    fail: function () {
                        util.isError('上传失败', that);
                    }
                })
            }
        })
    },
    dateAvatar2: function () {
        var that = this;
        wx.chooseImage({
            count: 1,
            success: function (res) {
                var tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: 'http://www.ppxpc.com/api/wx/common/imageUpload',
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {

                    },
                    success: function (result) {
                        if (result.statusCode == 200){
                            wx.showToast({
                                title: '上传成功',
                                icon: 'success',
                                duration: 2000
                            })
                            that.setData({
                                img2:result.data
                            })
                        }
                    },
                    fail: function () {
                        util.isError('上传失败', that);
                    }
                })
            }
        })
    },
    formSubmit: function (e) {
        var that = this;
        var data = e.detail.value
        data.img1=that.data.img1
        data.img2=that.data.img2
        data.userId=app.globalData.sk
        data.id=that.data.did
        util.req("/wx/driversInfo/save", data, function (result) {
            if (result.code == 1) {
                wx.showToast({
                    title: '操作成功',
                    icon: 'success',
                    duration: 2000
                })
                app.setDriversLicenseInfo(result.content)
            } else {
                util.isError(data.msg, that);
            }
        })
    },
    onLoad: function () {
        var that = this
        util.req("/wx/driversInfo/getInfo", {userId:app.globalData.sk}, function (result) {
            if (result.content != null) {
                that.setData({
                    url:util.imgurl,
                    number:result.content.number,
                    carType:result.content.carType,
                    registrationDate:result.content.registrationDate,
                    expiryDate:result.content.expiryDate,
                    img1:result.content.img1,
                    img2:result.content.img2,
                    did:result.content.id,
                })
            } else {
                that.setData({
                    url:util.imgurl
                })
                util.isError(data.msg, that);
            }
        })
        wx.getStorage({
            key: 'userInfo',
            success: function (res) {

            },
            fail: function (res) {
                app.login();
            }
        })
    }
})